package com.yao.sdk.net;

import android.content.Context;
import android.util.Log;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.yao.sdk.core.YaoSDK;
import com.yao.sdk.encrypt.Encrypt;
import com.yao.sdk.net.volley.CookieRequest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Created by Administrator on 2015/12/1.
 */
public class NetWorkControl {

    RequestQueue requestQueue;

    //List<NetWorkListener> netWorkListenerList = new CopyOnWriteArrayList<>();

    private static NetWorkControl instance = new NetWorkControl();

    public static NetWorkControl instance() {
        return instance;
    }

    public void init(Context context){
        requestQueue = Volley.newRequestQueue(context);
    }

    /*public void registerNetWorkListener(NetWorkListener listener) {
        if (!netWorkListenerList.contains(listener)) {
            netWorkListenerList.add(listener);
        }
    }

    public void unregisterNetWorkListener(NetWorkListener listener) {
        if (netWorkListenerList.contains(listener)) {
            netWorkListenerList.remove(listener);
        }
    }*/

    private void notifyNetWorkListeners(NetWorkListener listener, ResponseInfo responseInfo) {
        if (listener != null) {
            listener.onResponse(responseInfo);
        }

        /*for (NetWorkListener listener : netWorkListenerList) {
            listener.onResponse(url, responseInfo);
        }*/
    }


    public void request(NetWorkListener listener, final String url, Map<String, String> params,String token) {
        request(listener, url, params, null,token);
    }

    public void request(NetWorkListener listener, final String url, Map<String, String> params, Map<String, String> header,String token) {
        request(listener, url, null, params, header,token);
    }

    public void request(final NetWorkListener listener, final String url, NetConfig.RequestMethod requestMethod, final Map<String, String> params, Map<String, String> header,String token) {
        int requestMethodParam = NetWork.config().getRequestMethod();
        if (requestMethod != null) {
            requestMethodParam = requestMethod.getCode();
        }

        CookieRequest cookieRequest = new CookieRequest(requestMethodParam, url, new CookieRequest.CookieListener() {
            @Override
            public void onSuccess(CookieRequest.ResultSet resultSet) {
                ResponseInfo responseInfo = new ResponseInfo();
                responseInfo.isOk = true;
                responseInfo.header = resultSet.responseHeader;
                responseInfo.content = resultSet.responseInfo;
                responseInfo.params=params;
                responseInfo.url=url;
                notifyNetWorkListeners(listener, responseInfo);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

                ResponseInfo responseInfo = new ResponseInfo();
                responseInfo.isOk = false;
                responseInfo.error.errorInfo = volleyError.toString()+volleyError.getMessage();
                responseInfo.params=params;
                responseInfo.url=url;
                if (volleyError.toString().contains("com.android.volley.NoConnectionError")){
                    responseInfo.noConnection=true;
                }
                notifyNetWorkListeners(listener, responseInfo);
                Log.e("zwj","..........volley接口请求失败url="+responseInfo.url);
            }
        });
        Map<String,String> encParams=new LinkedHashMap<>();
        encParams.putAll(params);
        Encrypt encrypt=new Encrypt();
        String result=encrypt.toEncrppt(params,token);
        encParams.clear();
        encParams.put("_param", result);
        cookieRequest.setRetryPolicy(new DefaultRetryPolicy(10 * 1000, 2, 1.0f));
        if (encParams != null) {
            cookieRequest.setParam(encParams);
        }
        if (header != null) {
            cookieRequest.setHeader(header);
        }
        if (requestQueue == null) {
            requestQueue = Volley.newRequestQueue(YaoSDK.getApplication());
        }
        requestQueue.add(cookieRequest);
//        requestQueue.start();
    }
    public  void cancelRequest(Context mContext) {
        requestQueue.cancelAll(mContext.getClass().getSimpleName());
    }
}
